package engine.systems.ui.progressBar
{
	import engine.classes.enumerations.Axis;
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.update.GameSystem;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.text.TextField;
	
	/**
	 * The Progress Bar System scales the child display of a Render display to match the current
	 * progress of a component property value that the Progress Bar is monitoring.
	 * 
	 * @see ProgressBar
	 * @see ProgressBarNode
	 */
	public class ProgressBarSystem extends GameSystem
	{
		private var value:Number;
		private var container:DisplayObjectContainer;
		private var text:TextField;
		
		private var max:Number;
		private var progress:Number;
		private var percent:Number;
		private var bar:DisplayObject;
		
		public function ProgressBarSystem()
		{
			super(ProgressBarNode, updateNode);
			
			this.priority = SystemPriority.UPDATE;
		}
		
		private function updateNode(node:ProgressBarNode, time:Number):void
		{
			//Get the current value of component property the Progress Bar is monitoring.
			value = node.progress.component[node.progress.property];
			
			//The Render display needs to be cast as a Display Object Container.
			container = node.render.display as DisplayObjectContainer;
			
			/**
			 * Some progresses might not be the simple 0-1 variety. The Progress Bar's min and max
			 * values act as this 0-1 ratio.
			 */
			max 		= node.progress.max - node.progress.min;
			progress 	= value - node.progress.min;
			
			//Get the current progress.
			percent = progress / max;
			
			//Make sure the progress percent stays withing 0-1.
			if(percent < 0) 		percent = 0;
			else if(percent > 1)	percent = 1;
			
			//Get the child Display Object with the given name.
			bar = container.getChildByName(node.progress.childName);
			
			//Scale it on the given axis.
			switch(node.progress.axis)
			{
				case Axis.X:
					if(bar.scaleX != percent)
						bar.scaleX = percent;
					break;
				
				case Axis.Y:
					if(bar.scaleY != percent)
						bar.scaleY = percent;
					break;
			}
		}
	}
}